import store from '../../store'
import * as echarts from 'echarts'
import router from '../../router'

export default {
  name: 'Asset',
  data () {
    return {
      positionData: [
        {}
      ],
      capitalData: {}
    }
  },
  mounted () {
    store.dispatch('getCapital', store.state.user.id)
      .then(response => {
        this.positionData = response.data.positionData
        this.capitalData = response.data.capitalData
        this.drawPie(this.capitalData)
      })
      .catch(error => {
        console.log('connect failed')
        console.log(error)
      })
  },
  methods: {
    drawPie (rawData) {
      let myChart = echarts.getInstanceByDom(document.getElementById('myChart'))
      if (myChart == null) {
        myChart = echarts.init(document.getElementById('myChart'))
      }
      myChart.clear()
      myChart.setOption(
        {
          title: {
            text: '',
            subtext: '',
            left: 'center'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{b} : {c} ({d}%)'
          },
          legend: {
            orient: 'vertical',
            left: 'left'
          },
          color: ['#ff99cc', '#FF8C00'],
          series: [
            {
              name: '',
              type: 'pie',
              radius: '55%',
              center: ['50%', '60%'],
              data: [
                {
                  value: rawData.可用资金,
                  name: '资金'
                },
                {
                  value: rawData.总市值,
                  name: '股票'
                }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
      )
    },
    buyPosition (row) {
      router.push({
        path: '/Trade',
        query: {
          stockCode: row.stockCode,
          tradeType: '买入'
        }
      })
    },
    sellPosition (row) {
      router.push({
        path: '/Trade',
        query: {
          stockCode: row.stockCode,
          tradeType: '卖出'
        }
      })
    }
  }
}
